# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=gdb
pkgver=8.2.1
pkgrel=3
_gcc_ver=9.1.0
pkgdesc="GNU Debugger (MSYS2 version)"
arch=('i686' 'x86_64')
license=('GPL3')
url="https://www.gnu.org/software/gdb/"
groups=('base-devel')
depends=("libiconv" "zlib" "expat" "python3" "libexpat" "libreadline")
#checkdepends=('dejagnu' 'bc')
makedepends=("libiconv-devel" "zlib-devel" "ncurses-devel" "liblzma-devel" "libexpat-devel" "libreadline-devel")
options=('staticlibs' '!distcc' '!ccache')
source=("https://ftp.gnu.org/gnu/gdb/gdb-${pkgver}.tar.xz"{,.sig}
        'gdbinit'
        0001-Teach-gdb-how-to-unwind-cygwin-_sigbe-and-sigdelayed.patch
        0002-7.8-windows-nat-cygwin.patch
        0003-Better-handling-for-realpath-failures-in-windows_mak.patch
        0004-7.8-symtab-cygwin.patch
        0005-Remove-a-spurious-target_terminal_ours-from-windows_.patch
        0006-PR-build-21187-Static-linking-of-libstdc-and-libgcc-.patch
        0007-Fix-Cygwin-compilation-after-target_ops-C-conversion.patch
        'gdb-7.11.1-msysize.patch')
validpgpkeys=('F40ADB902B24264AA42E50BF92EDB04BFF325CF3')
sha256sums=('0a6a432907a03c5c8eaad3c3cffd50c00a40c3a5e3c4039440624bae703f2202'
            'SKIP'
            '2bbe7eddb1828c394d0ff99777058df79b1a596172603bb0e30d983fc1ea8785'
            '2bdd02d6dc46d1186a3fa4485922d277686ca9ff827dde6b9fa1a90e9345c953'
            '42051344b0f3103cf4006114a223a2cbe24369145a69a298d4f389ffffddab95'
            '91994d62047bb76688a785c9a911f8deda6e7b7aa258aade0a6b29904a9e9b58'
            '41a5f72e2ac5838d853b906e1611e844e164e05f18bf2be4674a3a47ca41a875'
            '7540630512c5fec56d330ffd05a8714d3a037080e1ff049eaa3c81aaae597708'
            'f46337e3b0750f562ad6dbd4ad28860df160e98583b541d609ad9fadaff357c9'
            '6e257ca1d46e76b87d2166f0cb302685a58ea0aa3a26ccdc3a6a17f730946db9'
            'bc8ecb4f7cb4213d653cd0d3811a8bfa771d8523f20fac33d7ae89b0361603b5')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}

  patch -p1 -i ${srcdir}/0001-Teach-gdb-how-to-unwind-cygwin-_sigbe-and-sigdelayed.patch
  patch -p1 -i ${srcdir}/0002-7.8-windows-nat-cygwin.patch
  patch -p1 -i ${srcdir}/0003-Better-handling-for-realpath-failures-in-windows_mak.patch
  patch -p1 -i ${srcdir}/0004-7.8-symtab-cygwin.patch
  patch -p1 -i ${srcdir}/0005-Remove-a-spurious-target_terminal_ours-from-windows_.patch
  patch -p1 -i ${srcdir}/0006-PR-build-21187-Static-linking-of-libstdc-and-libgcc-.patch
  patch -p1 -i ${srcdir}/0007-Fix-Cygwin-compilation-after-target_ops-C-conversion.patch
  patch -p1 -i ${srcdir}/gdb-7.11.1-msysize.patch

  # hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure
}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}
  ./configure \
    --build=${CHOST} \
    --host=${CHOST} \
    --prefix=/usr \
    --enable-64-bit-bfd \
    --disable-werror \
    --disable-staticlib \
    --with-system-gdbinit=/etc/gdbinit \
    --with-python=/usr/bin/python3 \
    --without-tcl \
    --without-tk \
    --without-guile \
    --with-expat \
    --with-lzma \
    --with-system-readline \
    --with-libiconv-prefix=/usr \
    --with-zlib \
    --enable-tui
  make
}

check() {
  cd ${srcdir}/${pkgname}-${pkgver}
  make check
}

package() {
  cd ${srcdir}/${pkgname}-${pkgver}
  make DESTDIR=${pkgdir} install

  # install "custom" system gdbinit
  install -D -m644 ${srcdir}/gdbinit ${pkgdir}/etc/gdbinit
  sed -i 's|%GCC_NAME%|gcc-'${_gcc_ver}'|g' ${pkgdir}/etc/gdbinit

  # these are shipped by binutils
  rm -fr ${pkgdir}/usr/{include,lib}/ ${pkgdir}/usr/share/locale/
  rm -f ${pkgdir}/usr/share/info/{bfd,configure,standards}.info
}
